/*
 * @(#)ResearchWorkflowStepOwnerAction.java
 *
 * Copyright (c) 2004 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRecordDAObject;
import com.dcivision.user.dao.UserRoleDAObject;
import com.dcivision.workflow.bean.MtmWfStepNotificationTarget;
import com.dcivision.workflow.bean.MtmWorkflowStepUserActor;
import com.dcivision.workflow.bean.WorkflowStep;
import com.dcivision.workflow.core.WorkflowRetrievalManager;
import com.dcivision.workflow.dao.MtmWorkflowInterruptUserActorDAObject;
import com.dcivision.workflow.dao.WorkflowStepDAObject;

/**
  MaintWorkflowInterruptRecordUserAction.java

  This class is for maint user role.

    @author          Dick.xie
    @company         DCIVision Limited
    @creation date   31/12/2004
    @version         $Revision: 1.6.2.1 $
*/

public class ResearchWorkflowStepOwnerAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.6.2.1 $";


  /** Creates a new instance of MaintWorkflowAssignProgressUserAction */
  public ResearchWorkflowStepOwnerAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.workflow.dao.WorkflowProgressDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.WORKFLOW_TASK);
  }

  /**
   * clearUniqueFields
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void clearUniqueFields(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintWorkflowProgressForm workflowProgressForm = (MaintWorkflowProgressForm) form;
    workflowProgressForm.setID(null);
  }

  /**
   * getObjectType
   *
   * @return  The corresponding object type of permission setup action.
   */
  public String getObjectType() {
    return(GlobalConstant.OBJECT_TYPE_WORKFLOW);
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    AbstractActionForm maintForm = (AbstractActionForm)form;
    MaintWorkflowProgressForm workflowProgressForm =(MaintWorkflowProgressForm)maintForm;
    log.debug("at AssignAction nav Mode: "+maintForm.getNavMode() + ", op Mode: "+maintForm.getOpMode());

    String opMode = (maintForm!=null) ? maintForm.getOpMode() : request.getParameter("opMode");    //maintForm.getOpMode();
    String navMode = (maintForm!=null) ? maintForm.getNavMode() : request.getParameter("navMode"); //maintForm.getNavMode();
    String notificationOwnerListValue = request.getParameter("notificationOwnerListValue");
    String parentStepID = request.getParameter("parentStepID");
    String currentStepID = request.getParameter("currentStepID");
    log.debug("The workflowRecord is notificationRuleListValue Submitter::" + notificationOwnerListValue);
    log.debug( ((maintForm!=null) ? maintForm.getClass().getName() : "")+", OP Mode:"+opMode+", Nav Mode:"+navMode);

    // Set the function code for current action.
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);  if (forward!=null) {return forward;}

    WorkflowRetrievalManager retrievalManager = new WorkflowRetrievalManager(this.getSessionContainer(request), this.getConnection(request));
    MtmWorkflowInterruptUserActorDAObject interruptActorDAO = new MtmWorkflowInterruptUserActorDAObject(this.getSessionContainer(request), this.getConnection(request));
    Locale locale = this.getSessionContainer(request).getSessionLocale();
    List allStepNameList = new ArrayList();
    if (!Utility.isEmpty(request.getParameter("wfRecordID")) && !Utility.isEmpty(parentStepID)) {
      allStepNameList = this.getNotifyOwnerByWorkflowRecordID(new Integer(request.getParameter("wfRecordID")), new Integer(parentStepID), request);
    }
    String[][] ACTOR_TYPE_MAP = null;
    if(Utility.isEmpty(currentStepID)) {
      ACTOR_TYPE_MAP = new String[allStepNameList.size() + 6][2];
    } else {
      ACTOR_TYPE_MAP = new String[allStepNameList.size() + 5][2];
    }
    ACTOR_TYPE_MAP[0][0] = MessageResourcesFactory.getMessage(locale, "workflow.label.everyone");
    ACTOR_TYPE_MAP[0][1] = "0"+"$" + ACTOR_TYPE_MAP[0][0] + "$" + MtmWorkflowStepUserActor.ACTOR_TYPE_EVERYONE;
    
    ACTOR_TYPE_MAP[1][0] = MessageResourcesFactory.getMessage(locale, "workflow.label.submitter");
    ACTOR_TYPE_MAP[1][1] = "0"+"$" + ACTOR_TYPE_MAP[1][0] + "$" + MtmWorkflowStepUserActor.ACTOR_TYPE_SUBMITTER;

    ACTOR_TYPE_MAP[2][0] = MessageResourcesFactory.getMessage(locale, "workflow.label.report_to");
    ACTOR_TYPE_MAP[2][1] = "0"+"$" + ACTOR_TYPE_MAP[2][0] + "$" +  MtmWorkflowStepUserActor.ACTOR_TYPE_REPORT_TO;

//    ACTOR_TYPE_MAP[2][0] = MessageResourcesFactory.getMessage(locale, "workflow.label.previous_task_owner");
//    ACTOR_TYPE_MAP[2][1] = MtmWorkflowStepUserActor.ACTOR_TYPE_PREVIOUS_TASK_OWNER;

    ACTOR_TYPE_MAP[3][0] = MessageResourcesFactory.getMessage(locale, "workflow.label.previous_task_action_taker");
    ACTOR_TYPE_MAP[3][1] = "0"+"$" + ACTOR_TYPE_MAP[3][0] + "$" + MtmWorkflowStepUserActor.ACTOR_TYPE_PREVIOUS_TASK_ACTION_TAKER;

    ACTOR_TYPE_MAP[4][0] = MessageResourcesFactory.getMessage(locale, "workflow.label.previous_task_owner");
    ACTOR_TYPE_MAP[4][1] = "0"+"$" + ACTOR_TYPE_MAP[4][0] + "$" + MtmWorkflowStepUserActor.ACTOR_TYPE_PREVIOUS_TASK_OWNER;

    for ( int i = 0; i < allStepNameList.size(); i++ ) {
      String[] stepName = (String[])allStepNameList.get(i);
      ACTOR_TYPE_MAP[i + 5][0] = stepName[1];
      ACTOR_TYPE_MAP[i + 5][1] = stepName[0];
    }
    if(Utility.isEmpty(currentStepID)) {
      ACTOR_TYPE_MAP[ACTOR_TYPE_MAP.length-1][0] = MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale(), "workflow.label.notify_current_step_owner");
      ACTOR_TYPE_MAP[ACTOR_TYPE_MAP.length-1][1] = "-1"+"$"+ACTOR_TYPE_MAP[ACTOR_TYPE_MAP.length-1][0] + "$" + MtmWfStepNotificationTarget.TARGET_TYPE_OWNER;
    }

    request.setAttribute("ACTOR_TYPE_MAP",ACTOR_TYPE_MAP);
    List userList = null;
    List groupList = null;
    List roleList = null;
    List notifyUserList = new ArrayList();
    List notifyGroupList = new ArrayList();
    List notifyRoleList = new ArrayList();
    List notifyActorTypesList = new ArrayList();
    Integer wfRecordID = null;
    if (((!Utility.isEmpty(notificationOwnerListValue))  && (!"null".equals(notificationOwnerListValue)))) {
      String[] notificationOwnerArr = TextUtility.splitString(notificationOwnerListValue,":");
      for (int k = 0; !Utility.isEmpty(notificationOwnerArr) && k < notificationOwnerArr.length; k++) {
        String[] ownerArr = TextUtility.splitString(notificationOwnerArr[k],"$");
//        log.debug("The submitter option::::" + ownerArr[0] + "   " + ownerArr[1]);
        if (MtmWorkflowStepUserActor.ACTOR_TYPE_USER.equals(ownerArr[2])) {
          notifyUserList.add(ownerArr[0]+"$"+ownerArr[1]);
        } else if (MtmWorkflowStepUserActor.ACTOR_TYPE_GROUP.equals(ownerArr[2])) {
          notifyGroupList.add(ownerArr[0]+"$"+ownerArr[1]);
        } else if (MtmWorkflowStepUserActor.ACTOR_TYPE_ROLE.equals(ownerArr[2])) {
          notifyRoleList.add(ownerArr[0]+"$"+ownerArr[1]);
        } else if (MtmWorkflowStepUserActor.ACTOR_TYPE_ACTION_TAKER.equals(ownerArr[2]) || MtmWorkflowStepUserActor.ACTOR_TYPE_REPORT_TO_OF_ACTION_TAKER.equals(ownerArr[2])) {
          notifyActorTypesList.add(notificationOwnerArr[k]);
          log.debug("ownerArr[1] + '$'+ ownerArr[0]:::"+ownerArr[1] + "$" + ownerArr[0]);
        } else  {
          notifyActorTypesList.add(notificationOwnerArr[k]);
        }
      }
    }
      if (!Utility.isEmpty(notifyUserList)) {
      String[] userListStr = new String[notifyUserList.size()];
      for(int j = 0; j < notifyUserList.size(); j++) {
        userListStr[j] = (String)notifyUserList.get(j);
      }
      workflowProgressForm.setProgressUser(userListStr);
    }
    if (!Utility.isEmpty(notifyRoleList)) {
      String[] roleListStr = new String[notifyRoleList.size()];
      for(int j = 0; j < notifyRoleList.size(); j++) {
        roleListStr[j] = (String)notifyRoleList.get(j);
      }
      workflowProgressForm.setProgressRole(roleListStr);
    }
    if (!Utility.isEmpty(notifyGroupList)) {
      String[] groupListStr = new String[notifyGroupList.size()];
      for(int j = 0; j < notifyGroupList.size(); j++) {
        groupListStr[j] = (String)notifyGroupList.get(j);
      }
      workflowProgressForm.setProgressGroup(groupListStr);
    }
    if (!Utility.isEmpty(notifyActorTypesList)) {
      String[] actorTypesListStr = new String[notifyActorTypesList.size()];
      for(int j = 0; j < notifyActorTypesList.size(); j++) {
        actorTypesListStr[j] = (String)notifyActorTypesList.get(j);
      }
      workflowProgressForm.setActorTypes(actorTypesListStr);
    }
    wfRecordID=new Integer(request.getParameter("wfRecordID"));
    if (!Utility.isEmpty(wfRecordID)) {
      workflowProgressForm.setWorkflowRecordID(wfRecordID.toString());
    }
    UserRecordDAObject userDAO = new UserRecordDAObject(this.getSessionContainer(request),this.getConnection(request));
    UserGroupDAObject groupDAO = new UserGroupDAObject(this.getSessionContainer(request),this.getConnection(request));
    UserRoleDAObject roleDAO = new UserRoleDAObject(this.getSessionContainer(request),this.getConnection(request));

    userList = userDAO.getFullList();
    groupList = groupDAO.getFullList();
    roleList = roleDAO.getFullList();
    request.setAttribute("userList", userList);
    request.setAttribute("groupList", groupList);
    request.setAttribute("roleList", roleList);
    //return super.execute(mapping, form, request, response)
    return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
  }

  
  public List getNotifyOwnerByWorkflowRecordID(Integer workflowRecordID, Integer stepID, HttpServletRequest request) throws ApplicationException{
    WorkflowStepDAObject workflowStepDAO = new WorkflowStepDAObject(this.getSessionContainer(request), this.getConnection(request));
    WorkflowRetrievalManager retrievalManager = new WorkflowRetrievalManager(this.getSessionContainer(request), this.getConnection(request));
    List workflowStepList = new ArrayList();
    List parentStepList = new ArrayList();
    parentStepList = retrievalManager.getSuperStepList(stepID, parentStepList);
    workflowStepList = workflowStepDAO.getListByWorkflowRecordID(workflowRecordID);
    WorkflowStep currentStep = (WorkflowStep) workflowStepDAO.getObjectByID(stepID);
    List workflowStepNameList = new ArrayList();
    if (!Utility.isEmpty(parentStepList)) {
      for (int i= 0; i < parentStepList.size(); i++) {
        WorkflowStep workflowStep = (WorkflowStep)parentStepList.get(i);
        if ( !WorkflowStep.ACTION_TYPE_SUBMIT.equals(workflowStep.getActionType()) && !WorkflowStep.ACTION_TYPE_END.equals(workflowStep.getActionType()) && !WorkflowStep.ACTION_TYPE_SYSTEM_AUTO.equals(workflowStep.getActionType())) {
          String[] aOption = new String[2];
          //For Previous Action Taker
          aOption[1] = MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale(), "workflow.label.n_step_action_taker", workflowStep.getStepSeq() + ". " + workflowStep.getStepName());
          aOption[0] = workflowStep.getID().toString()+"$" + aOption[1] + "$" + MtmWorkflowStepUserActor.ACTOR_TYPE_ACTION_TAKER;
          workflowStepNameList.add(aOption);
        }
      }
    }
    if ( !WorkflowStep.ACTION_TYPE_SUBMIT.equals(currentStep.getActionType()) &&  !WorkflowStep.ACTION_TYPE_SYSTEM_AUTO.equals(currentStep.getActionType())) {
      String[] currentStepOption = new String[2];
      // Previous Action Taker
      currentStepOption[1] = MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale(), "workflow.label.n_step_action_taker", currentStep.getStepSeq() + ". " + currentStep.getStepName());
      currentStepOption[0] = currentStep.getID().toString()+"$" + currentStepOption[1] + "$" + MtmWorkflowStepUserActor.ACTOR_TYPE_ACTION_TAKER;
      workflowStepNameList.add(currentStepOption);
    }
    if(!Utility.isEmpty(workflowStepList) ){
      for(int i = 0;i < workflowStepList.size();i++){
        WorkflowStep workflowStep = (WorkflowStep)workflowStepList.get(i);
        if (!WorkflowStep.ACTION_TYPE_END.equals(workflowStep.getActionType()) && !WorkflowStep.ACTION_TYPE_SYSTEM_AUTO.equals(workflowStep.getActionType())) {
          String[] aOption = new String[2];
          //For Previous Action Taker's Report To
          aOption = new String[2];
          aOption[1] = MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale(), "workflow.label.n_step_owner", workflowStep.getStepSeq() + ". " + workflowStep.getStepName());
          aOption[0] = workflowStep.getID().toString()+"$"+aOption[1] + "$" + MtmWfStepNotificationTarget.TARGET_TYPE_OWNER;
          workflowStepNameList.add(aOption);
       }
     }
   }
    return workflowStepNameList;
  }
}
